import datetime
import sys
from gglib.logger import BaseLogger, LOG_LEVEL


class FileHandler(BaseLogger):
    def __init__(self, level, fmt, file=None):
        super().__init__(level, fmt)
        if not file:
            self._file = sys.stdout
        else:
            self._file = open(file, "w")

    def __output_log(self, level, sender, message):
        if LOG_LEVEL[level] >= LOG_LEVEL[self.level]:
            self._file.write("{} <{}> [{}]: {}\n".format(datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
                                                     sender,
                                                     level,
                                                     message))

    def debug(self, sender, *args):
        self.__output_log("debug", sender, *args)

    def info(self, sender, *args):
        self.__output_log("info", sender, *args)

    def warn(self, sender, *args):
        self.__output_log("warn", sender, *args)

    def error(self, sender, *args):
        self.__output_log("error", sender, *args)
